package engine.planner;

import compiler.parse.DbParse;
import engine.query.Plan;

import storage.tx.Transaction;
public class Planner {
	private QueryPlanner qplanner;
	private UpdatePlanner uplanner;
	
	public Planner(QueryPlanner qplanner, UpdatePlanner uplanner) {
		this.qplanner = qplanner;
		this.uplanner = uplanner;
	}
	
	public Plan createQueryPlan(QueryData data, Transaction tx) {
		return qplanner.createPlan(data, tx);
	}
	public Plan createQueryPlan(String viewdef,Transaction tx){
		DbParse dbparse = new DbParse("",System.out);
		Object o = dbparse.translate(viewdef);
		if(o instanceof QueryData){
			QueryData cvd = (QueryData)o;
			return qplanner.createPlan(cvd, tx);
		}
		else
			return null;
	}
	public int executeUpdate(Object obj, Transaction tx) {
		if (obj instanceof InsertData)
			return uplanner.executeInsert((InsertData)obj, tx);
		else if (obj instanceof DeleteData)
			return uplanner.executeDelete((DeleteData)obj, tx);
		else if (obj instanceof ModifyData)
			return uplanner.executeModify((ModifyData)obj, tx);
		else if (obj instanceof CreateTableData)
			return uplanner.executeCreateTable((CreateTableData)obj, tx);
		else if (obj instanceof CreateViewData)
			return uplanner.executeCreateView((CreateViewData)obj, tx);
		else if (obj instanceof CreateIndexData)
			return uplanner.executeCreateIndex((CreateIndexData)obj, tx);
		else
			return 0;
	}
}
